const replyPosting = require("../models/replyPosting")
const posting = require("../models/posting")
const user = require("../models/userDetail")
const message = require("../models/message")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            let inform = () => {
                return new Promise(function (resolve, reject) {
                    if (fields.authorRole == '0' && fields.postOrNot == true) {
                        user.find({
                            userGroup: "2"  //0 for test, 2 for production
                        }, {
                            UUID: 1
                        }, (err, docs) => {
                            if (err) {
                                console.log({ 'state': false, 'msg': error });
                            } else {
                                let idx = Math.floor(Math.random() * docs.length)
                                let target_uuid = docs[idx].UUID

                                let description = ""
                                let mulu = fields.menu.split(',')

                                if (mulu[0] == 'js') description += "JavaScript"
                                else if (mulu[0] == 'wx') description += "微信小程序"
                                else if (mulu[0] == 'ydhlw') description += "移动互联网"

                                description += "部分 第" + (parseInt(mulu[1]) + 1) + "章 第" + (parseInt(mulu[2]) + 1) + "节 知识点：" + fields.point_title + " 有同学提问，速求答疑！"

                                message.create({
                                    type: "answer_notification",
                                    content: description,
                                    UUID: target_uuid,
                                    target: "",
                                    result: "",
                                    status: "unread"
                                }, (error, doc) => {
                                    if (error) {
                                        console.log({ 'state': false, 'msg': error });
                                    } else {
                                        console.log({ 'state': true, 'msg': "ok" });
                                        resolve()
                                    }
                                })
                            }
                        })
                    } else {
                        console.log({ 'state': true, 'msg': "reply" });
                        resolve()
                    }
                })
            }

            let __posting = () => {
                if (fields.postOrNot == true) {
                    console.log(fields.content);
                    posting.create({
                        menu: fields.menu,
                        point_title: fields.point_title,
                        point_id: fields.point_id,
                        content: fields.content,
                        authorUUID: fields.authorUUID,
                        authorUsername: fields.authorUsername,
                        authorRole: fields.authorRole,
                        likeCount: 0,
                        time: fields.time,
                        avatar: fields.avatar
                    }, (error, doc) => {
                        if (error) {
                            res.json({ 'state': false, 'msg': error });
                        } else {
                            let return_doc = {
                                menu: doc.menu,
                                point_title: doc.point_title,
                                point_id: doc.point_id,
                                content: doc.content,
                                authorUUID: doc.authorUUID,
                                authorUsername: doc.authorUsername,
                                authorRole: doc.authorRole,
                                likeCount: doc.likeCount,
                                time: doc.time,
                                avatar: doc.avatar,
                                _id: doc._id,
                                reply: []
                            }
                            res.json({ 'state': true, 'posting': return_doc });
                        }
                    })
                } else if (fields.postOrNot == false) {
                    // console.log(fields)
                    replyPosting.create({
                        postingId: fields.postingId,
                        content: fields.content,
                        authorUUID: fields.authorUUID,
                        authorUsername:fields.authorUsername,
                        authorRole: fields.authorRole,
                        replyToName: fields.replyToName,
                        replyToUUID: fields.replyToUUID,
                        replyToId: fields.replyToId,
                        time: fields.time,
                        likeCount: 0,
                        avatar: fields.avatar
                    }, (error, doc) => {
                        if (error) {
                            res.json({ 'state': false, 'msg': error });
                        } else {
                            res.json({ 'state': true, 'msg': doc });
                        }
                    })
                }
            }

            inform().then(__posting)

        }

    });

});

module.exports = router;